package nl.hr.minor.mobilelife.j1213.jlr.words;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.DragEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewManager;

public class DropZone extends View implements View.OnDragListener {

	public DropZone(Context context) {
		super(context);
		this.setBackgroundColor(Color.GREEN);
		setOnDragListener(this);
	}

	@Override
	public boolean onDrag(View v, DragEvent event) {
		// TODO Auto-generated method stub

		final int action = event.getAction();

		if(v == this){
			switch(action){
			case DragEvent.ACTION_DROP:


				ViewGroup viewGroup = (ViewGroup) v.getParent();
				int index = viewGroup.indexOfChild(v);



				View viewOne = (View) event.getLocalState();
				ViewGroup group = (ViewGroup) viewOne.getParent();
				int indextwo = group.indexOfChild(viewOne);

				if(viewGroup.equals(group)){
					ArrayList<View> viewList = new ArrayList<View>();
					int viewCount = viewGroup.getChildCount();
					for(int i = 0; i < viewCount; i++){
						viewList.add(viewGroup.getChildAt(i));
					}
					viewGroup.removeAllViews();
					viewList.set(indextwo, v);
					viewList.set(index, viewOne);
					viewGroup.removeAllViews();
					for(int i = 0; i < viewCount; i++){
						viewGroup.addView(viewList.get(i));
					}

				}else{
					group.removeView(viewOne);
					viewGroup.removeView(v);

					viewGroup.addView(viewOne, index);
					group.addView(v, indextwo);	
				}
				break;
			}
		}

		return true;
	}


}
